package com.yin.myproject;

import javax.persistence.*;
import java.lang.annotation.Annotation;

/**
 * Class对象:对注解的操作
 *
 * @author Eason
 * @create 2018-03-31 21:33
 **/
public class ClassAPIForAnnotation {
    public static void main(String[] args) {
        Class<User> clazz = User.class;

        System.out.println("测试getAnnotations()方法");
        //获取当前类上定义的注解对象(包括父类,但父类注解必须是可继承的,简单来讲,父类注解必须标注@Inherited)
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("注解名称:" + annotation.toString());
        }
        System.out.println("===========================================");
        System.out.println("getDeclaredAnnotations()方法");
        //获取当前类上定义的注解对象
        Annotation[] declaredAnnotations = clazz.getDeclaredAnnotations();
        for (Annotation annotation : declaredAnnotations) {
            System.out.println("注解名称:" + annotation.toString());
        }
    }

    @MappedSuperclass
    @AnnotationParent
    class BaseEntity {

        private Long version;

        public Long getVersion() {
            return version;
        }

        public void setVersion(Long version) {
            this.version = version;
        }
    }

    @Entity
    @Table(name = "user")
    @AnnotationTest1
    class User extends BaseEntity {
        @Id
        private String id;
        @Column(name = "first_name", nullable = false, unique = true)
        private String firstName;
        @Column(name = "middle_name", nullable = false, unique = true)
        private String middleName;
        @Column(name = "last_name", nullable = false, unique = true)
        private String lastName;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getMiddleName() {
            return middleName;
        }

        public void setMiddleName(String middleName) {
            this.middleName = middleName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}
